package com.syyo.gulimall.product.app;

//import org.springframework.security.access.prepost.PreAuthorize;

import com.syyo.common.domain.ResultVo;
import com.syyo.common.utils.ResultUtils;
import com.syyo.gulimall.product.domain.req.AttrReq;
import com.syyo.gulimall.product.domain.req.AttrSpuReq;
import com.syyo.gulimall.product.service.AttrService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 商品属性
 *
 * @author syyo
 * @email syyo@gmail.com
 * @date 2021-01-20 09:32:12
 */
@RestController
@RequestMapping("/product/attr")
public class AttrController {

    @Autowired
    private AttrService attrService;

    /**
     * 新增
     */
    @PostMapping
    // @PreAuthorize("@syyo.check('system:attr:add')")
    public ResultVo add(@RequestBody AttrReq req) {
        return ResultUtils.ok(attrService.add(req));
    }

    /**
     * 删除
     */
    @DeleteMapping("/{id}")
    // @PreAuthorize("@syyo.check('system:attr:del')")
    public ResultVo del(@PathVariable("id") Integer id) {
        return ResultUtils.ok(attrService.del(id));
    }

    /**
     * 修改
     */
    @PutMapping
    // @PreAuthorize("@syyo.check('system:attr:edit')")
    public ResultVo edit(@RequestBody AttrReq req) {
        return ResultUtils.ok(attrService.edit(req));
    }

    /**
     * 修改
     */
    @PutMapping("/{spuId}")
    // @PreAuthorize("@syyo.check('system:attr:edit')")
    public ResultVo editSpuAttr(@PathVariable("spuId") Long spuId, @RequestBody List<AttrSpuReq> req) {
        return ResultUtils.ok(attrService.editSpuAttr(spuId, req));
    }

    /**
     * 信息
     */
    @GetMapping("/{id}")
    public ResultVo findOne(@PathVariable("id") Integer id) {
        return ResultUtils.ok(attrService.findOne(id));
    }

    /**
     * 列表
     */
    @GetMapping("/list/{type}/{pageNum}/{pageSize}")
    public ResultVo findPage(@PathVariable("pageNum") Integer pageNum,
                             @PathVariable("pageSize") Integer pageSize,
                             @PathVariable("type") String type,
                             AttrReq req) {
        return ResultUtils.ok(attrService.findPage(pageNum, pageSize, req, type));
    }

    /**
     * 查询商品规格属性
     */
    @GetMapping("/base/listforspu/{spuId}")
    public ResultVo findAttr(@PathVariable("spuId") Integer spuId) {
        return ResultUtils.ok(attrService.findAttr(spuId));
    }
}
